package com.example.demo.course.repository;

import com.example.demo.course.UserCourse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

public interface UserCourseRepository extends JpaRepository<UserCourse, Long> {

    List<UserCourse> findByUserId(Long userId);

    List<UserCourse> findByCourseId(Long courseId);

    boolean existsByUserIdAndCourseId(Long userId, Long courseId);

    @Query("SELECT u.email FROM User u JOIN UserCourse uc ON u.id = uc.userId WHERE uc.courseId = :courseId")
    List<String> findUserEmailsByCourseId(@Param("courseId") Long courseId);

    @Query("SELECT uc.courseId FROM UserCourse uc WHERE uc.userId = :userId")
    List<Long> findCourseIdsByUserId(@Param("userId") Long userId);

    @Query(value = "SELECT COUNT(*) FROM user_course WHERE course_id = :courseId",
            nativeQuery = true)
    Integer countByCourseIdNative(@Param("courseId") Long courseId);
}